package com.demo.mantis.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.techarts.dragonfly.app.helper.Converter;

/**
 * Servlet implementation class CalculatorServlet
 */
@WebServlet("/calculator")
public class CalculatorServlet extends HttpServlet {
	 
	@Override
	    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	        float x = Converter.toFloat(request.getParameter("x"));
	        float y = Converter.toFloat(request.getParameter("y"));
	        String op = request.getParameter("op").toLowerCase();
	        response.setContentType("application/json");
	        switch (op) {
	        case "plus":
	            var result = x + y;
	            response.getWriter().write(result + "");
	            break;
	        case "subtract":
	            result = x - y;
	            response.getWriter().write(result + "");   
	            break;
	        case "multiply":
	            result = x * y;
	            response.getWriter().write(result + "");
	            break;
	        case "divide":
	            if (y != 0) {
	                float a = x / y;
	                response.getWriter().write(a + "");
	            } else {
	                response.getWriter().write("\"不能为零\"");
	                break;
	            }
	        default:
	            response.getWriter().write("\"不支持改方法\"");
	        }
	    }

}
